package com.igroupes.ovineherd.model;

import cn.hutool.core.util.EnumUtil;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.enums.BeanTypeKey;
import com.igroupes.ovineherd.model.table.TableAttribute;
import lombok.Getter;
import lombok.Setter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Getter
@Setter
@Component
public class ModelData {

    private TableAttribute table;
    private List<IParentModelData<? extends BasicAttribute>> fieldModel;

    public ModelData() {
        this.table = new TableAttribute();
        this.fieldModel = getFieldTemplate();
    }

    private List<IParentModelData<? extends BasicAttribute>> getFieldTemplate() {
        List<IParentModelData<? extends BasicAttribute>> list = new ArrayList<>();
        Map<String, BeanTypeKey> enumMap = EnumUtil.getEnumMap(BeanTypeKey.class);

        for (BeanTypeKey item : enumMap.values()) {
            try {

                Class<?> classes = Class.forName(item.getBean());
                BasicAttribute basicAttribute = (BasicAttribute) classes.newInstance();
                list.add(new IParentModelData<>(item.getName(),item.getDesc(),item.getKey()
                        ,basicAttribute));
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
                throw new ModelException("对象异常" + item.getBean());
            }
        };
        return list;
    }

}
